//
//  YZMWeichatImageViewController.m
//  YZMicroStock
//
//  Created by apple on 17/1/3.
//  Copyright © 2017年 cqjr. All rights reserved.
//

#import "YZMWeichatImageViewController.h"
#import "YZMShareService.h"
#import "YZMImageActionView.h"

@interface YZMWeichatImageViewController ()
@property(nonatomic, strong) UIImageView *imageView;
@property(nonatomic, strong) UILabel *textLabel;

@end

@implementation YZMWeichatImageViewController

- (id)init {
    if (self = [super init]) {
        self.view.backgroundColor = [YZMAppStyle appBackgroundColor];
        self.title = @"关注微信";
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createImageAndLabel];
    // Do any additional setup after loading the view.
}

-(void)createImageAndLabel{
    
    UIView *bgView=[UIView new];
    [self.view addSubview:bgView];
    bgView.backgroundColor=[YZMAppStyle colorForMain];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.view).offset(63);
        make.height.mas_equalTo(276);
    }];
    
    self.imageView=[[UIImageView alloc]init];
    [self.imageView setImage:[UIImage imageNamed:@"icon_weichatNumber"]];
    [self.view addSubview:_imageView];
    
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(138);
        make.centerX.equalTo(self.view);
        make.top.mas_equalTo(self.view).offset(126);
    }];
    self.imageView.userInteractionEnabled=YES;
    
    UILongPressGestureRecognizer *longPress=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(imageAction:)];
    [self.imageView addGestureRecognizer:longPress];
    
    self.textLabel=[YZMAppStyle labelWithLargeWhite];
    [self.view addSubview:_textLabel];
    _textLabel.text=@"微信扫一扫，关注就有好礼哦~";
    _textLabel.textAlignment=NSTextAlignmentCenter;
    [_textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.imageView.mas_bottom).offset(35);
        make.centerX.equalTo(self.view);
    }];
}

-(void)imageAction:(UILongPressGestureRecognizer *)sender{
    
    if ([sender state]==UIGestureRecognizerStateBegan) {
        YZMImageActionView *imageActionView=[YZMImageActionView new];
        NSMutableArray *array=[NSMutableArray arrayWithObjects:@"分享给朋友",@"保存到相册",@"取消", nil];

        if (![[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"weixin://"]]) {
            array=[NSMutableArray arrayWithObjects:@"保存到相册",@"取消", nil];
        }
        
        imageActionView.datasource=[NSMutableArray arrayWithArray:array];
        
        imageActionView.shareBlock=^(){
            [[YZMShareService sharedService]shareTextToPlatformType:UMSocialPlatformType_WechatSession withContent:@{@"image":_imageView.image}];
        };
        imageActionView.saveBlock=^(){
            [self saveImageToPhotos:_imageView.image];
        };
        [imageActionView show];
    }
}

- (void)saveImageToPhotos:(UIImage*)savedImage
{
    UIImageWriteToSavedPhotosAlbum(savedImage, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
    
}

- (void)image: (UIImage *) image didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo
{
    NSString *msg = nil ;
    
    if(error != NULL){
        
        msg = @"保存图片失败" ;
    }else{
        
        msg = @"保存图片成功" ;
    }
    
    [MBProgressHUD showMessage:msg];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
